\documentclass[11pt]{article}
\usepackage{alltt}
\usepackage{url}
\usepackage{hyperref}

\title{CS2223, HW1\\  Intro to Lua} 
\author{Joshua D. Guttman} 
\date{Due 1pm, Friday, 29 Oct}

\begin{document}
\maketitle

\paragraph{Download and install} Lua version 5.1 on some convenient
machine.  Pre-compiled binaries for various systems are available at
\url{http://lua-users.org/wiki/LuaBinaries}.

For documentation, see \url{http://www.lua.org/docs.html}.  There is
an online manual at \url{http://www.lua.org/manual/5.1/}, and I've
bought three copies of the published book version to put on reserve in
the library.

The book \emph{Programming in Lua} is very good, and not expensive.
The previous version, describing Lua 5.0 rather than 5.1, is freely
available online at \url{http://www.lua.org/pil/}.  I think you will
find it very usable.

For our purposes, the most important addition in Lua 5.1 versus 5.0 is
the operator \verb+#+, which gives the length of an array without
gaps.  If \verb+a+ is an array with entries for all the numbers
$1,\ldots,k$, and no other numbers, then \verb+#a+ returns the number
$k$.

You can give Lua interactive commands by starting the program
\verb+lua+.  A command \verb+print(function_name(args))+ will apply
the function to its arguments and print the result.  The command
\verb+dofile(filename)+ executes the declarations and commands
contained within the file inside the current Lua session.

You can also execute a file as a unit (at least on Unix-family
systems) by inserting the text 
%
\begin{alltt}
   #!/usr/local/bin/lua 
\end{alltt}
% 
as the first line (substituting the path that leads to the Lua
interpreter on your system if it is different).  If you add \verb+-i+
as a flag at the end, it will start an interactive session after
executing the file contents.  

\paragraph{Read and play with} the file \url{tbibtex.lua} and its
input file, \url{lua_input.bib}.  This is a modified version of a
bibtex citation file.  Notice how little code is needed to turn a
large, fairly complicated mass of data into a usable form.  The code:

\begin{alltt}
articles = \{\}

function Article (t) 
   articles[t.tag] = t
end

dofile("lua_input.bib") 
\end{alltt}
%
defines a function Article that lets Lua treat each item 
%
\begin{alltt}
Article\{
  tag =		 ...citation tag..., 
  author = 	 ...author names...,
  ...
\}
\end{alltt}
%
as a function applied to a Lua \emph{table} within curly braces \{\}.
Now you can walk through all of the entries using the code:
%
\begin{alltt}
   for tag,entry in pairs (articles) do ... end 
\end{alltt}
%
Each time through the loop, executing the code \verb+...+, the
identifiers \verb+tag+ and \verb+entry+ will be bound to another
citation tag and the whole record it belongs to.

You can also write normal numeric for-loops and while-loops in the
forms:  
%
\begin{alltt}
   for i = 1, last do ... end 

   while (expression) do ... end 
\end{alltt}
%
The for-loop executes \verb+...+ once with \verb+i+ bound to each of
the values from 1 to \verb+last+, including the last.  The while loop
executes repeatedly as long as \verb+expression+ evaluates to a value
that is different from \verb+false+ and \verb+nil+.  See section 4.3
in the Lua book for examples and more information (pp.~30--33).

\paragraph{Build} a number of Lua functions to determine how many
papers cited in \url{lua_input.bib} have each number of authors.

Start by taking everything from \url{tbibtex.lua}.  Then:
%
\begin{enumerate}
  \item Suppose that \verb+t+ is an entry with \verb+tag+ and
  \verb+author+ fields, etc.  Write a function to return how many 
  authors are listed in \verb+t+'s author field, \verb+t.author+.  

  Use the functions \verb+get_first_author+ and \verb+get_next_author+
  in \url{lua_input.bib}, which look for the string `` and '' as a
  separator between the authors within a string.

  Make sure that your function returns the right value (namely 0), if
  the entry has \emph{no} author field because \verb+t.author+ is
  \verb+nil+.  Don't let it raise an error in this case.  
  \item Declare an array \verb+author_numbers+ to hold the number of
  articles that have any given number of authors.  Your declaration
  can simply initialize a variable to an empty table \verb+{}+.
  You'll fill in its contents in the next step.
  \item Write a function to walk through the table of \verb+articles+.
  Call the function from step 1 on each article, and if it has $n$
  authors, add one to the value in \verb+author_numbers+'s slot for
  $n$.  Initialize the slot if needed.
  \item When you have filled in all of \verb+author_numbers+, write a
  function to print a summary of it.  For each author number, print 
  % 
\begin{alltt}
   There are k articles with i authors  
\end{alltt}
  % 
  for each of the possible numbers of authors $i$ and the correct
  number of articles.
\end{enumerate}

\paragraph{Extra Credit.}  Print out a graph of the summary
information.  For each number of authors $i$, print a column of $k$
$X$s.

An example showing the output for a different input is in the file
\url{sample_graph.txt}.  In that file, the $x$-axis is ``number of
authors,'' and the $y$-axis is the number of articles having that many
authors.  If you prefer, you may also print the graph horizontally, so
that the width of a row shows the number of articles with a given
number of authors.  In this case, you may want to use one character to
represent two or three articles, so that it doesn't become too wide. 


To direct the output to a file, use \verb+io.output(filename)+.  To
write $X$s and blanks, you can use \verb+io.write(... args ...)+.
Also useful is \verb+string.format+.


\end{document}


%Localwords: Lua online

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% End: 
